# Create your views here.
from django.db import transaction
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from FmyTest.models import versioncontrol

@csrf_exempt
@transaction.atomic
def stats_control(request):
    if request.method == "POST":
        try:
            rid = request.POST['rid']
            data = versioncontrol.objects.get(id=rid)
            if data.stats == 's':
                data.stats = 'f'
            else:
                data.stats = 's'
            data.save()
            return JsonResponse({'status': True, 'messages': '状态修改成功！'})

        except Exception as ex:
            print(ex, '######################')

    return JsonResponse({'status': True, 'messages': '状态修改失败！'})

def getversion(request):
    # 获取字段的值
    results = versioncontrol.objects.filter(stats='s').values_list('soft_version', flat=True)
    print(results,'results')
    data = {
        'options': [{'value': i, 'label': i} for i in list(results) ]
    }
    if not data['options']:
        data = {
            'options': [{'value': "", 'label': "目前没有可用的版本号"}]
        }
    return JsonResponse(data)